package com.zzx.test.future;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: TestClass1
 * @Description: JDK9新特性测试类
 * @Author ZhouZhiXin
 * @Date 2022/7/8
 * @Version 1.0
 */
public class NineClass1 {

    @Test
    public void test1() {
        // jdk9新增的集合增强方法，用来创建不可变集合，即一旦创建就无法再执行添加、删除、替换、排序等操作
        List<String> stringList1 = List.of("a", "b", "c");
        Set<String> stringSet1 = Set.of("a", "b", "c");
        Map<String, Integer> map1 = Map.of("a", 1, "b", 2, "c", 3);
        List<String> stringList2 = List.copyOf(stringList1);
        stringList2.add("d");
    }

    @Test
    public void test2() {
        int read;
        String filePath = "e:\\news1.txt";
        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            // readAllBytes 读取 InputStream 中的所有剩余字节
            for (byte b : fileInputStream.readAllBytes()) {
                System.out.println(b);
            }
            System.out.println("---");
            // readNBytes 读取指定数量的字节到数组中
            for (byte b : fileInputStream.readNBytes(2)) {
                System.out.println(b);
            }
            System.out.println("---");
            // transferTo 读取 InputStream 中的全部字节并写入到指定的 OutputStream 中
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            fileInputStream.transferTo(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
